﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using AutoMapper;
using Microsoft.Web.Mvc;
using SapaFinance.DTO;
using SapaFinance.Services;
using Telerik.Web.Mvc;
using Telerik.Web.Mvc.UI;

namespace SapaFinance.UI.Controllers
{
    public class CorporationController : Controller
    {
        private readonly ICorporationService _corporationService;

        public CorporationController(ICorporationService corporationService)
        {
            _corporationService = corporationService;
        }

        public virtual ActionResult Index()
        {
            return View("Index",_corporationService.GetCorporationList());
        }
        public virtual ActionResult RenderPayer(Guid id)
        {
            return View("EditorTemplates/PayerList", _corporationService.GetPayerSelectedListNonCash(id));
        }

        public virtual ActionResult RenderPayerByPaymentType(Guid id)
        {
            return View("EditorTemplates/PayerList", _corporationService.GetPayerSelectedListByPaymentType(id));
        }


        public virtual ActionResult RenderPayee(Guid id)
        {
            return View("EditorTemplates/PayeeList", _corporationService.GetAllCorporationSelectedList(id));
        }

       

        public virtual ActionResult RenderCorporation(Guid id)
        {
            return View("EditorTemplates/CorporationList", _corporationService.GetAllCorporationSelectedList(id));
        }

        private void ValidateDto(CorporationDTO dto)
        {
            if (dto.SelectedAgent.Equals(Guid.Empty)) ModelState.AddModelError("SelectedAgent", "Агент должен быть определен");
        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Insert(CorporationDTO corporationDTO, List<TreeViewItem> CFOTree_checkedNodes)
        {
            corporationDTO.CheckedCfo = CFOTree_checkedNodes;
            corporationDTO.CurrentUser = User.Identity.Name;
            ValidateDto(corporationDTO);

            if (ModelState.IsValid){
                _corporationService.CreateCorporation(corporationDTO);
                return this.RedirectToAction(x => x.Index());
            }
            return Index();
        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Delete(Guid id)
        {
            _corporationService.DeleteCorporation(id);
            return this.RedirectToAction(x => x.Index());
        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Update(CorporationDTO corporationDTO, List<TreeViewItem> CFOTree_checkedNodes)
        {
            corporationDTO.CurrentUser = User.Identity.Name;
            corporationDTO.CheckedCfo = CFOTree_checkedNodes;
            ValidateDto(corporationDTO);
            if (ModelState.IsValid){
                _corporationService.UpdateCorporation(corporationDTO);
                return this.RedirectToAction(x => x.Index());
            }
            return Index();
        }

    }
}